package com.lancoo.taskerdemo.network;

import com.facebook.stetho.okhttp3.StethoInterceptor;

import java.util.concurrent.TimeUnit;

import okhttp3.OkHttpClient;
import retrofit2.Retrofit;
import retrofit2.adapter.rxjava.RxJavaCallAdapterFactory;
import retrofit2.converter.gson.GsonConverterFactory;

/**
 * Created by gongwq on 2017/6/19 0019.
 */

public class NetWork {
    private static String newsBaseUrl = "http://v.juhe.cn/";


    private static OkHttpClient okHttpClient = new OkHttpClient
            .Builder()
            //有网时使用的拦截器
            .addNetworkInterceptor(new StethoInterceptor())//调试用
            //所有情况下使用的拦截器
            // .addInterceptor(new UrlInterceptor())
            /**
             * default timeout
             * connectTimeout = 10_000;
             * readTimeout = 10_000;
             * writeTimeout = 10_000;
             */
            .connectTimeout(20_000, TimeUnit.SECONDS)
            .readTimeout(20_000, TimeUnit.SECONDS)
            .writeTimeout(20_000, TimeUnit.SECONDS)
            .build();

    private static Retrofit.Builder getBuilder() {
        return new Retrofit.Builder()
                .client(okHttpClient)
                .addConverterFactory(GsonConverterFactory.create())
                .addConverterFactory(FastJsonConverterFactory.create())
                .addCallAdapterFactory(RxJavaCallAdapterFactory.create());
    }

    /**
     * @return 内部接口数据获取
     */
    public static Retrofit getNewsRetrofit() {
        return getBuilder().baseUrl(newsBaseUrl).build();
    }

    public static NewsApi getNewsApi() {
        return getNewsRetrofit().create(NewsApi.class);
    }
}
